package com.demo.controller;

/**
 * @program: demo
 * @description:
 * @author: 陈程猿
 * @create: 2025-03-02 19:34
 **/

import com.demo.annotation.GlobalInterceptor;
import com.demo.annotation.VerifyParam;
import com.demo.entity.po.Category;
import com.demo.entity.query.CategoryQuery;
import com.demo.entity.vo.ResponseVO;
import com.demo.service.CategoryService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController("categoryController")
@RequestMapping("/category")
public class CategoryController extends ABaseController {

    @Resource
    private CategoryService categoryService;


    /**
     * 根据条件分页查询
     */
    @RequestMapping("/loadAllCategory")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO loadAllCategory(CategoryQuery query) {
        query.setOrderBy("sort asc");
        return getSuccessResponseVO(categoryService.findListByParam(query));
    }

    /**
     * 保存分类信息
     *
     * @param category
     * @return
     */
    @RequestMapping("/saveCategory")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO saveCategory(Category category) {
        categoryService.saveCategory(category);
        return getSuccessResponseVO(null);
    }


    /**
     * 删除分类信息
     *
     * @param categoryId 分类id
     * @return
     */
    @RequestMapping("/delCategory")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO delCategory(@VerifyParam(required = true) Integer categoryId) {
        categoryService.deleteCategoryByCategoryId(categoryId);
        return getSuccessResponseVO(null);
    }

    /**
     * 改变排序
     *
     * @param categoryIds
     * @return
     */
    @RequestMapping("/changeSort")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO changeSort(@VerifyParam(required = true) String categoryIds) {
        categoryService.changeSort(categoryIds);
        return getSuccessResponseVO(null);
    }

    /**
     * 查询根据序号查询分类
     * @return
     */
    @RequestMapping("/loadAllCategory4Select")
    @GlobalInterceptor(checkLogin = false)
    public ResponseVO loadAllCategory4Select(CategoryQuery categoryQuery) {
        categoryQuery.setOrderBy("sort asc");
        List<Category> list = categoryService.findListByParam(categoryQuery);
        return getSuccessResponseVO(list);
    }

}